//
//  LLHomeViewController.swift
//  新浪微博
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 mac.com. All rights reserved.
//

import UIKit
import SVProgressHUD
let LLid = "cell"
class LLHomeViewController: LLBaseTableViewController {
//模型数组
   lazy var status:LLStatusListViewModel = LLStatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userInf {
        
            loginView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)
            return
        }
       
        setNavbar()
        preparForTableview()
        loadData()
    }
    private func preparForTableview(){

        tableView.registerClass(LLTableViewCell.self, forCellReuseIdentifier: LLid)
//    tableView.rowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估行高  行高的自动计算
        //提高tableView加载效率 预估行高 * 数据源的数量 = contentSize  -> tableView才可以滚动
        tableView.estimatedRowHeight = 300
        tableView.separatorStyle = .None
        tableView.tableFooterView = pullupView
//        tableView.scrollsToTop = true
        pullupView.startAnimating()
//        refreshControl = UIRefreshControl()
        tableView.addSubview(refreshView)
        refreshView.addTarget(self, action: Selector("loadData"), forControlEvents: .ValueChanged)
    }
    private func setNavbar(){
        let popItem = UIBarButtonItem(imagename: "navigationbar_pop", target: self, action: Selector("push"))
    navigationItem.rightBarButtonItem = popItem
    
    }
   @objc private func push(){
        let detail = LLDetailViewController()
        detail.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(detail, animated: true)
    }
    //拉加载下拉刷新控件
    lazy var refreshView:LLRefreshControl = LLRefreshControl()
    //懒加载上拉刷新控件
    lazy var pullupView:UIActivityIndicatorView = {
    let act = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        act.sizeToFit()
        return act
    }()
    
// MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.status.statusViewModelArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        let cell = tableView.dequeueReusableCellWithIdentifier(LLid, forIndexPath: indexPath) as! LLTableViewCell
        cell.statu = status.statusViewModelArray[indexPath.row]
        if indexPath.row == status.statusViewModelArray.count - 1 && !pullupView.isAnimating()
        {
        print("开始静默加载")
            pullupView.startAnimating()
            loadData()
        }
//        print(status.statusViewModelArray[indexPath.row].status)
        return cell
        
    }
    
    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}
extension LLHomeViewController {
    //加载数据
    @objc private func loadData() {
        status.loadData (pullupView.isAnimating()){ (isSusscess) in
            //停止小菊花的转动
            self.pullupView.stopAnimating()
//            self.refreshControl?.endRefreshing()
            self.refreshView.endrefresh()
            if !isSusscess {
                //提示用户请求失败
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            
            //请求数据成功
            //刷新数据
            self.tableView.reloadData()
        }
    }
}





